fedora core 6 1.2949 + vserver 2.2.0
[linux-2.6.git] / ipc / shm.c
1 /*
2  * linux/ipc/shm.c
3  * Copyright (C) 1992, 1993 Krishna Balasubramanian
4  *       Many improvements/fixes by Bruno Haible.
5  * Replaced `struct shm_desc' by `struct vm_area_struct', July 1994.
6  * Fixed the shm swap deallocation (shm_unuse()), August 1998 Andrea Arcangeli.
7  *
8  * /proc/sysvipc/shm support (c) 1999 Dragos Acostachioaie <dragos@iname.com>
9  * BIGMEM support, Andrea Arcangeli <andrea@suse.de>
10  * SMP thread shm, Jean-Luc Boyard <jean-luc.boyard@siemens.fr>
11  * HIGHMEM support, Ingo Molnar <mingo@redhat.com>
12  * Make shmmax, shmall, shmmni sysctl'able, Christoph Rohland <cr@sap.com>
13  * Shared /dev/zero support, Kanoj Sarcar <kanoj@sgi.com>
14  * Move the mm functionality over to mm/shmem.c, Christoph Rohland <cr@sap.com>
15  *
16  * support for audit of ipc object properties and permission changes
17  * Dustin Kirkland <dustin.kirkland@us.ibm.com>
18  *
19  * namespaces support
20  * OpenVZ, SWsoft Inc.
21  * Pavel Emelianov <xemul@openvz.org>
22  */
23
24 #include <linux/slab.h>
25 #include <linux/mm.h>
26 #include <linux/hugetlb.h>
27 #include <linux/shm.h>
28 #include <linux/init.h>
29 #include <linux/file.h>
30 #include <linux/mman.h>
31 #include <linux/shmem_fs.h>
32 #include <linux/security.h>
33 #include <linux/syscalls.h>
34 #include <linux/audit.h>
35 #include <linux/capability.h>
36 #include <linux/ptrace.h>
37 #include <linux/seq_file.h>
38 #include <linux/mutex.h>
39 #include <linux/nsproxy.h>
40 #include <linux/vs_context.h>
41 #include <linux/vs_limit.h>
42
43 #include <asm/uaccess.h>
44
45 #include "util.h"
46
47 static struct file_operations shm_file_operations;
48 static struct vm_operations_struct shm_vm_ops;
49
50 static struct ipc_ids init_shm_ids;
51
52 #define shm_ids(ns)     (*((ns)->ids[IPC_SHM_IDS]))
53
54 #define shm_lock(ns, id)                \
55         ((struct shmid_kernel*)ipc_lock(&shm_ids(ns),id))
56 #define shm_unlock(shp)                 \
57         ipc_unlock(&(shp)->shm_perm)
58 #define shm_get(ns, id)                 \
59         ((struct shmid_kernel*)ipc_get(&shm_ids(ns),id))
60 #define shm_buildid(ns, id, seq)        \
61         ipc_buildid(&shm_ids(ns), id, seq)
62
63 static int newseg (struct ipc_namespace *ns, key_t key,
64                 int shmflg, size_t size);
65 static void shm_open (struct vm_area_struct *shmd);
66 static void shm_close (struct vm_area_struct *shmd);
67 static void shm_destroy (struct ipc_namespace *ns, struct shmid_kernel *shp);
68 #ifdef CONFIG_PROC_FS
69 static int sysvipc_shm_proc_show(struct seq_file *s, void *it);
70 #endif
71
72 static void __ipc_init __shm_init_ns(struct ipc_namespace *ns, struct ipc_ids *ids)
73 {
74         ns->ids[IPC_SHM_IDS] = ids;
75         ns->shm_ctlmax = SHMMAX;
76         ns->shm_ctlall = SHMALL;
77         ns->shm_ctlmni = SHMMNI;
78         ns->shm_tot = 0;
79         ipc_init_ids(ids, 1);
80 }
81
82 static void do_shm_rmid(struct ipc_namespace *ns, struct shmid_kernel *shp)
83 {
84         if (shp->shm_nattch){
85                 shp->shm_perm.mode |= SHM_DEST;
86                 /* Do not find it any more */
87                 shp->shm_perm.key = IPC_PRIVATE;
88                 shm_unlock(shp);
89         } else
90                 shm_destroy(ns, shp);
91 }
92
93 #ifdef CONFIG_IPC_NS
94 int shm_init_ns(struct ipc_namespace *ns)
95 {
96         struct ipc_ids *ids;
97
98         ids = kmalloc(sizeof(struct ipc_ids), GFP_KERNEL);
99         if (ids == NULL)
100                 return -ENOMEM;
101
102         __shm_init_ns(ns, ids);
103         return 0;
104 }
105
106 void shm_exit_ns(struct ipc_namespace *ns)
107 {
108         int i;
109         struct shmid_kernel *shp;
110
111         mutex_lock(&shm_ids(ns).mutex);
112         for (i = 0; i <= shm_ids(ns).max_id; i++) {
113                 shp = shm_lock(ns, i);
114                 if (shp == NULL)
115                         continue;
116
117                 do_shm_rmid(ns, shp);
118         }
119         mutex_unlock(&shm_ids(ns).mutex);
120
121         ipc_fini_ids(ns->ids[IPC_SHM_IDS]);
122         kfree(ns->ids[IPC_SHM_IDS]);
123         ns->ids[IPC_SHM_IDS] = NULL;
124 }
125 #endif
126
127 void __init shm_init (void)
128 {
129         __shm_init_ns(&init_ipc_ns, &init_shm_ids);
130         ipc_init_proc_interface("sysvipc/shm",
131                                 "       key      shmid perms       size  cpid  lpid nattch   uid   gid  cuid  cgid      atime      dtime      ctime\n",
132                                 IPC_SHM_IDS, sysvipc_shm_proc_show);
133 }
134
135 static inline int shm_checkid(struct ipc_namespace *ns,
136                 struct shmid_kernel *s, int id)
137 {
138         if (ipc_checkid(&shm_ids(ns), &s->shm_perm, id))
139                 return -EIDRM;
140         return 0;
141 }
142
143 static inline struct shmid_kernel *shm_rmid(struct ipc_namespace *ns, int id)
144 {
145         return (struct shmid_kernel *)ipc_rmid(&shm_ids(ns), id);
146 }
147
148 static inline int shm_addid(struct ipc_namespace *ns, struct shmid_kernel *shp)
149 {
150         return ipc_addid(&shm_ids(ns), &shp->shm_perm, ns->shm_ctlmni);
151 }
152
153
154
155 static inline void shm_inc(struct ipc_namespace *ns, int id)
156 {
157         struct shmid_kernel *shp;
158
159         shp = shm_lock(ns, id);
160         BUG_ON(!shp);
161         shp->shm_atim = get_seconds();
162         shp->shm_lprid = current->tgid;
163         shp->shm_nattch++;
164         shm_unlock(shp);
165 }
166
167 #define shm_file_ns(file) (*((struct ipc_namespace **)&(file)->private_data))
168
169 /* This is called by fork, once for every shm attach. */
170 static void shm_open(struct vm_area_struct *shmd)
171 {
172         shm_inc(shm_file_ns(shmd->vm_file),
173                         shmd->vm_file->f_path.dentry->d_inode->i_ino);
174 }
175
176 /*
177  * shm_destroy - free the struct shmid_kernel
178  *
179  * @shp: struct to free
180  *
181  * It has to be called with shp and shm_ids.mutex locked,
182  * but returns with shp unlocked and freed.
183  */
184 static void shm_destroy(struct ipc_namespace *ns, struct shmid_kernel *shp)
185 {
186         struct vx_info *vxi = lookup_vx_info(shp->shm_perm.xid);
187         int numpages = (shp->shm_segsz + PAGE_SIZE - 1) >> PAGE_SHIFT;
188
189         vx_ipcshm_sub(vxi, shp, numpages);
190         ns->shm_tot -= numpages;
191
192         shm_rmid(ns, shp->id);
193         shm_unlock(shp);
194         if (!is_file_hugepages(shp->shm_file))
195                 shmem_lock(shp->shm_file, 0, shp->mlock_user);
196         else
197                 user_shm_unlock(shp->shm_file->f_path.dentry->d_inode->i_size,
198                                                 shp->mlock_user);
199         fput (shp->shm_file);
200         security_shm_free(shp);
201         put_vx_info(vxi);
202         ipc_rcu_putref(shp);
203 }
204
205 /*
206  * remove the attach descriptor shmd.
207  * free memory for segment if it is marked destroyed.
208  * The descriptor has already been removed from the current->mm->mmap list
209  * and will later be kfree()d.
210  */
211 static void shm_close (struct vm_area_struct *shmd)
212 {
213         struct file * file = shmd->vm_file;
214         int id = file->f_path.dentry->d_inode->i_ino;
215         struct shmid_kernel *shp;
216         struct ipc_namespace *ns;
217
218         ns = shm_file_ns(file);
219
220         mutex_lock(&shm_ids(ns).mutex);
221         /* remove from the list of attaches of the shm segment */
222         shp = shm_lock(ns, id);
223         BUG_ON(!shp);
224         shp->shm_lprid = current->tgid;
225         shp->shm_dtim = get_seconds();
226         shp->shm_nattch--;
227         if(shp->shm_nattch == 0 &&
228            shp->shm_perm.mode & SHM_DEST)
229                 shm_destroy(ns, shp);
230         else
231                 shm_unlock(shp);
232         mutex_unlock(&shm_ids(ns).mutex);
233 }
234
235 static int shm_mmap(struct file * file, struct vm_area_struct * vma)
236 {
237         int ret;
238
239         ret = shmem_mmap(file, vma);
240         if (ret == 0) {
241                 vma->vm_ops = &shm_vm_ops;
242                 if (!(vma->vm_flags & VM_WRITE))
243                         vma->vm_flags &= ~VM_MAYWRITE;
244                 shm_inc(shm_file_ns(file), file->f_path.dentry->d_inode->i_ino);
245         }
246
247         return ret;
248 }
249
250 static int shm_release(struct inode *ino, struct file *file)
251 {
252         struct ipc_namespace *ns;
253
254         ns = shm_file_ns(file);
255         put_ipc_ns(ns);
256         shm_file_ns(file) = NULL;
257         return 0;
258 }
259
260 static struct file_operations shm_file_operations = {
261         .mmap           = shm_mmap,
262         .release        = shm_release,
263 #ifndef CONFIG_MMU
264         .get_unmapped_area = shmem_get_unmapped_area,
265 #endif
266 };
267
268 static struct vm_operations_struct shm_vm_ops = {
269         .open   = shm_open,     /* callback for a new vm-area open */
270         .close  = shm_close,    /* callback for when the vm-area is released */
271         .nopage = shmem_nopage,
272 #if defined(CONFIG_NUMA) && defined(CONFIG_SHMEM)
273         .set_policy = shmem_set_policy,
274         .get_policy = shmem_get_policy,
275 #endif
276 };
277
278 static int newseg (struct ipc_namespace *ns, key_t key, int shmflg, size_t size)
279 {
280         int error;
281         struct shmid_kernel *shp;
282         int numpages = (size + PAGE_SIZE -1) >> PAGE_SHIFT;
283         struct file * file;
284         char name[13];
285         int id;
286
287         if (size < SHMMIN || size > ns->shm_ctlmax)
288                 return -EINVAL;
289
290         if (ns->shm_tot + numpages > ns->shm_ctlall)
291                 return -ENOSPC;
292
293         if (!vx_ipcshm_avail(current->vx_info, numpages))
294                 return -ENOSPC;
295
296         shp = ipc_rcu_alloc(sizeof(*shp));
297         if (!shp)
298                 return -ENOMEM;
299
300         shp->shm_perm.key = key;
301         shp->shm_perm.xid = vx_current_xid();
302         shp->shm_perm.mode = (shmflg & S_IRWXUGO);
303         shp->mlock_user = NULL;
304
305         shp->shm_perm.security = NULL;
306         error = security_shm_alloc(shp);
307         if (error) {
308                 ipc_rcu_putref(shp);
309                 return error;
310         }
311
312         if (shmflg & SHM_HUGETLB) {
313                 /* hugetlb_zero_setup takes care of mlock user accounting */
314                 file = hugetlb_zero_setup(size);
315                 shp->mlock_user = current->user;
316         } else {
317                 int acctflag = VM_ACCOUNT;
318                 /*
319                  * Do not allow no accounting for OVERCOMMIT_NEVER, even
320                  * if it's asked for.
321                  */
322                 if  ((shmflg & SHM_NORESERVE) &&
323                                 sysctl_overcommit_memory != OVERCOMMIT_NEVER)
324                         acctflag = 0;
325                 sprintf (name, "SYSV%08x", key);
326                 file = shmem_file_setup(name, size, acctflag);
327         }
328         error = PTR_ERR(file);
329         if (IS_ERR(file))
330                 goto no_file;
331
332         error = -ENOSPC;
333         id = shm_addid(ns, shp);
334         if(id == -1) 
335                 goto no_id;
336
337         shp->shm_cprid = current->tgid;
338         shp->shm_lprid = 0;
339         shp->shm_atim = shp->shm_dtim = 0;
340         shp->shm_ctim = get_seconds();
341         shp->shm_segsz = size;
342         shp->shm_nattch = 0;
343         shp->id = shm_buildid(ns, id, shp->shm_perm.seq);
344         shp->shm_file = file;
345         file->f_path.dentry->d_inode->i_ino = shp->id;
346
347         shm_file_ns(file) = get_ipc_ns(ns);
348
349         /* Hugetlb ops would have already been assigned. */
350         if (!(shmflg & SHM_HUGETLB))
351                 file->f_op = &shm_file_operations;
352
353         ns->shm_tot += numpages;
354         vx_ipcshm_add(current->vx_info, key, numpages);
355         shm_unlock(shp);
356         return shp->id;
357
358 no_id:
359         fput(file);
360 no_file:
361         security_shm_free(shp);
362         ipc_rcu_putref(shp);
363         return error;
364 }
365
366 asmlinkage long sys_shmget (key_t key, size_t size, int shmflg)
367 {
368         struct shmid_kernel *shp;
369         int err, id = 0;
370         struct ipc_namespace *ns;
371
372         ns = current->nsproxy->ipc_ns;
373
374         mutex_lock(&shm_ids(ns).mutex);
375         if (key == IPC_PRIVATE) {
376                 err = newseg(ns, key, shmflg, size);
377         } else if ((id = ipc_findkey(&shm_ids(ns), key)) == -1) {
378                 if (!(shmflg & IPC_CREAT))
379                         err = -ENOENT;
380                 else
381                         err = newseg(ns, key, shmflg, size);
382         } else if ((shmflg & IPC_CREAT) && (shmflg & IPC_EXCL)) {
383                 err = -EEXIST;
384         } else {
385                 shp = shm_lock(ns, id);
386                 BUG_ON(shp==NULL);
387                 if (shp->shm_segsz < size)
388                         err = -EINVAL;
389                 else if (ipcperms(&shp->shm_perm, shmflg))
390                         err = -EACCES;
391                 else {
392                         int shmid = shm_buildid(ns, id, shp->shm_perm.seq);
393                         err = security_shm_associate(shp, shmflg);
394                         if (!err)
395                                 err = shmid;
396                 }
397                 shm_unlock(shp);
398         }
399         mutex_unlock(&shm_ids(ns).mutex);
400
401         return err;
402 }
403
404 static inline unsigned long copy_shmid_to_user(void __user *buf, struct shmid64_ds *in, int version)
405 {
406         switch(version) {
407         case IPC_64:
408                 return copy_to_user(buf, in, sizeof(*in));
409         case IPC_OLD:
410             {
411                 struct shmid_ds out;
412
413                 ipc64_perm_to_ipc_perm(&in->shm_perm, &out.shm_perm);
414                 out.shm_segsz   = in->shm_segsz;
415                 out.shm_atime   = in->shm_atime;
416                 out.shm_dtime   = in->shm_dtime;
417                 out.shm_ctime   = in->shm_ctime;
418                 out.shm_cpid    = in->shm_cpid;
419                 out.shm_lpid    = in->shm_lpid;
420                 out.shm_nattch  = in->shm_nattch;
421
422                 return copy_to_user(buf, &out, sizeof(out));
423             }
424         default:
425                 return -EINVAL;
426         }
427 }
428
429 struct shm_setbuf {
430         uid_t   uid;
431         gid_t   gid;
432         mode_t  mode;
433 };      
434
435 static inline unsigned long copy_shmid_from_user(struct shm_setbuf *out, void __user *buf, int version)
436 {
437         switch(version) {
438         case IPC_64:
439             {
440                 struct shmid64_ds tbuf;
441
442                 if (copy_from_user(&tbuf, buf, sizeof(tbuf)))
443                         return -EFAULT;
444
445                 out->uid        = tbuf.shm_perm.uid;
446                 out->gid        = tbuf.shm_perm.gid;
447                 out->mode       = tbuf.shm_perm.mode;
448
449                 return 0;
450             }
451         case IPC_OLD:
452             {
453                 struct shmid_ds tbuf_old;
454
455                 if (copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
456                         return -EFAULT;
457
458                 out->uid        = tbuf_old.shm_perm.uid;
459                 out->gid        = tbuf_old.shm_perm.gid;
460                 out->mode       = tbuf_old.shm_perm.mode;
461
462                 return 0;
463             }
464         default:
465                 return -EINVAL;
466         }
467 }
468
469 static inline unsigned long copy_shminfo_to_user(void __user *buf, struct shminfo64 *in, int version)
470 {
471         switch(version) {
472         case IPC_64:
473                 return copy_to_user(buf, in, sizeof(*in));
474         case IPC_OLD:
475             {
476                 struct shminfo out;
477
478                 if(in->shmmax > INT_MAX)
479                         out.shmmax = INT_MAX;
480                 else
481                         out.shmmax = (int)in->shmmax;
482
483                 out.shmmin      = in->shmmin;
484                 out.shmmni      = in->shmmni;
485                 out.shmseg      = in->shmseg;
486                 out.shmall      = in->shmall; 
487
488                 return copy_to_user(buf, &out, sizeof(out));
489             }
490         default:
491                 return -EINVAL;
492         }
493 }
494
495 static void shm_get_stat(struct ipc_namespace *ns, unsigned long *rss,
496                 unsigned long *swp)
497 {
498         int i;
499
500         *rss = 0;
501         *swp = 0;
502
503         for (i = 0; i <= shm_ids(ns).max_id; i++) {
504                 struct shmid_kernel *shp;
505                 struct inode *inode;
506
507                 shp = shm_get(ns, i);
508                 if(!shp)
509                         continue;
510
511                 inode = shp->shm_file->f_path.dentry->d_inode;
512
513                 if (is_file_hugepages(shp->shm_file)) {
514                         struct address_space *mapping = inode->i_mapping;
515                         *rss += (HPAGE_SIZE/PAGE_SIZE)*mapping->nrpages;
516                 } else {
517                         struct shmem_inode_info *info = SHMEM_I(inode);
518                         spin_lock(&info->lock);
519                         *rss += inode->i_mapping->nrpages;
520                         *swp += info->swapped;
521                         spin_unlock(&info->lock);
522                 }
523         }
524 }
525
526 asmlinkage long sys_shmctl (int shmid, int cmd, struct shmid_ds __user *buf)
527 {
528         struct shm_setbuf setbuf;
529         struct shmid_kernel *shp;
530         int err, version;
531         struct ipc_namespace *ns;
532
533         if (cmd < 0 || shmid < 0) {
534                 err = -EINVAL;
535                 goto out;
536         }
537
538         version = ipc_parse_version(&cmd);
539         ns = current->nsproxy->ipc_ns;
540
541         switch (cmd) { /* replace with proc interface ? */
542         case IPC_INFO:
543         {
544                 struct shminfo64 shminfo;
545
546                 err = security_shm_shmctl(NULL, cmd);
547                 if (err)
548                         return err;
549
550                 memset(&shminfo,0,sizeof(shminfo));
551                 shminfo.shmmni = shminfo.shmseg = ns->shm_ctlmni;
552                 shminfo.shmmax = ns->shm_ctlmax;
553                 shminfo.shmall = ns->shm_ctlall;
554
555                 shminfo.shmmin = SHMMIN;
556                 if(copy_shminfo_to_user (buf, &shminfo, version))
557                         return -EFAULT;
558                 /* reading a integer is always atomic */
559                 err= shm_ids(ns).max_id;
560                 if(err<0)
561                         err = 0;
562                 goto out;
563         }
564         case SHM_INFO:
565         {
566                 struct shm_info shm_info;
567
568                 err = security_shm_shmctl(NULL, cmd);
569                 if (err)
570                         return err;
571
572                 memset(&shm_info,0,sizeof(shm_info));
573                 mutex_lock(&shm_ids(ns).mutex);
574                 shm_info.used_ids = shm_ids(ns).in_use;
575                 shm_get_stat (ns, &shm_info.shm_rss, &shm_info.shm_swp);
576                 shm_info.shm_tot = ns->shm_tot;
577                 shm_info.swap_attempts = 0;
578                 shm_info.swap_successes = 0;
579                 err = shm_ids(ns).max_id;
580                 mutex_unlock(&shm_ids(ns).mutex);
581                 if(copy_to_user (buf, &shm_info, sizeof(shm_info))) {
582                         err = -EFAULT;
583                         goto out;
584                 }
585
586                 err = err < 0 ? 0 : err;
587                 goto out;
588         }
589         case SHM_STAT:
590         case IPC_STAT:
591         {
592                 struct shmid64_ds tbuf;
593                 int result;
594                 memset(&tbuf, 0, sizeof(tbuf));
595                 shp = shm_lock(ns, shmid);
596                 if(shp==NULL) {
597                         err = -EINVAL;
598                         goto out;
599                 } else if(cmd==SHM_STAT) {
600                         err = -EINVAL;
601                         if (shmid > shm_ids(ns).max_id)
602                                 goto out_unlock;
603                         result = shm_buildid(ns, shmid, shp->shm_perm.seq);
604                 } else {
605                         err = shm_checkid(ns, shp,shmid);
606                         if(err)
607                                 goto out_unlock;
608                         result = 0;
609                 }
610                 err=-EACCES;
611                 if (ipcperms (&shp->shm_perm, S_IRUGO))
612                         goto out_unlock;
613                 err = security_shm_shmctl(shp, cmd);
614                 if (err)
615                         goto out_unlock;
616                 kernel_to_ipc64_perm(&shp->shm_perm, &tbuf.shm_perm);
617                 tbuf.shm_segsz  = shp->shm_segsz;
618                 tbuf.shm_atime  = shp->shm_atim;
619                 tbuf.shm_dtime  = shp->shm_dtim;
620                 tbuf.shm_ctime  = shp->shm_ctim;
621                 tbuf.shm_cpid   = shp->shm_cprid;
622                 tbuf.shm_lpid   = shp->shm_lprid;
623                 if (!is_file_hugepages(shp->shm_file))
624                         tbuf.shm_nattch = shp->shm_nattch;
625                 else
626                         tbuf.shm_nattch = file_count(shp->shm_file) - 1;
627                 shm_unlock(shp);
628                 if(copy_shmid_to_user (buf, &tbuf, version))
629                         err = -EFAULT;
630                 else
631                         err = result;
632                 goto out;
633         }
634         case SHM_LOCK:
635         case SHM_UNLOCK:
636         {
637                 shp = shm_lock(ns, shmid);
638                 if(shp==NULL) {
639                         err = -EINVAL;
640                         goto out;
641                 }
642                 err = shm_checkid(ns, shp,shmid);
643                 if(err)
644                         goto out_unlock;
645
646                 err = audit_ipc_obj(&(shp->shm_perm));
647                 if (err)
648                         goto out_unlock;
649
650                 if (!capable(CAP_IPC_LOCK)) {
651                         err = -EPERM;
652                         if (current->euid != shp->shm_perm.uid &&
653                             current->euid != shp->shm_perm.cuid)
654                                 goto out_unlock;
655                         if (cmd == SHM_LOCK &&
656                             !current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur)
657                                 goto out_unlock;
658                 }
659
660                 err = security_shm_shmctl(shp, cmd);
661                 if (err)
662                         goto out_unlock;
663                 
664                 if(cmd==SHM_LOCK) {
665                         struct user_struct * user = current->user;
666                         if (!is_file_hugepages(shp->shm_file)) {
667                                 err = shmem_lock(shp->shm_file, 1, user);
668                                 if (!err) {
669                                         shp->shm_perm.mode |= SHM_LOCKED;
670                                         shp->mlock_user = user;
671                                 }
672                         }
673                 } else if (!is_file_hugepages(shp->shm_file)) {
674                         shmem_lock(shp->shm_file, 0, shp->mlock_user);
675                         shp->shm_perm.mode &= ~SHM_LOCKED;
676                         shp->mlock_user = NULL;
677                 }
678                 shm_unlock(shp);
679                 goto out;
680         }
681         case IPC_RMID:
682         {
683                 /*
684                  *      We cannot simply remove the file. The SVID states
685                  *      that the block remains until the last person
686                  *      detaches from it, then is deleted. A shmat() on
687                  *      an RMID segment is legal in older Linux and if 
688                  *      we change it apps break...
689                  *
690                  *      Instead we set a destroyed flag, and then blow
691                  *      the name away when the usage hits zero.
692                  */
693                 mutex_lock(&shm_ids(ns).mutex);
694                 shp = shm_lock(ns, shmid);
695                 err = -EINVAL;
696                 if (shp == NULL) 
697                         goto out_up;
698                 err = shm_checkid(ns, shp, shmid);
699                 if(err)
700                         goto out_unlock_up;
701
702                 err = audit_ipc_obj(&(shp->shm_perm));
703                 if (err)
704                         goto out_unlock_up;
705
706                 if (current->euid != shp->shm_perm.uid &&
707                     current->euid != shp->shm_perm.cuid && 
708                     !capable(CAP_SYS_ADMIN)) {
709                         err=-EPERM;
710                         goto out_unlock_up;
711                 }
712
713                 err = security_shm_shmctl(shp, cmd);
714                 if (err)
715                         goto out_unlock_up;
716
717                 do_shm_rmid(ns, shp);
718                 mutex_unlock(&shm_ids(ns).mutex);
719                 goto out;
720         }
721
722         case IPC_SET:
723         {
724                 if (copy_shmid_from_user (&setbuf, buf, version)) {
725                         err = -EFAULT;
726                         goto out;
727                 }
728                 mutex_lock(&shm_ids(ns).mutex);
729                 shp = shm_lock(ns, shmid);
730                 err=-EINVAL;
731                 if(shp==NULL)
732                         goto out_up;
733                 err = shm_checkid(ns, shp,shmid);
734                 if(err)
735                         goto out_unlock_up;
736                 err = audit_ipc_obj(&(shp->shm_perm));
737                 if (err)
738                         goto out_unlock_up;
739                 err = audit_ipc_set_perm(0, setbuf.uid, setbuf.gid, setbuf.mode);
740                 if (err)
741                         goto out_unlock_up;
742                 err=-EPERM;
743                 if (current->euid != shp->shm_perm.uid &&
744                     current->euid != shp->shm_perm.cuid && 
745                     !capable(CAP_SYS_ADMIN)) {
746                         goto out_unlock_up;
747                 }
748
749                 err = security_shm_shmctl(shp, cmd);
750                 if (err)
751                         goto out_unlock_up;
752                 
753                 shp->shm_perm.uid = setbuf.uid;
754                 shp->shm_perm.gid = setbuf.gid;
755                 shp->shm_perm.mode = (shp->shm_perm.mode & ~S_IRWXUGO)
756                         | (setbuf.mode & S_IRWXUGO);
757                 shp->shm_ctim = get_seconds();
758                 break;
759         }
760
761         default:
762                 err = -EINVAL;
763                 goto out;
764         }
765
766         err = 0;
767 out_unlock_up:
768         shm_unlock(shp);
769 out_up:
770         mutex_unlock(&shm_ids(ns).mutex);
771         goto out;
772 out_unlock:
773         shm_unlock(shp);
774 out:
775         return err;
776 }
777
778 /*
779  * Fix shmaddr, allocate descriptor, map shm, add attach descriptor to lists.
780  *
781  * NOTE! Despite the name, this is NOT a direct system call entrypoint. The
782  * "raddr" thing points to kernel space, and there has to be a wrapper around
783  * this.
784  */
785 long do_shmat(int shmid, char __user *shmaddr, int shmflg, ulong *raddr)
786 {
787         struct shmid_kernel *shp;
788         unsigned long addr;
789         unsigned long size;
790         struct file * file;
791         int    err;
792         unsigned long flags;
793         unsigned long prot;
794         int acc_mode;
795         void *user_addr;
796         struct ipc_namespace *ns;
797
798         if (shmid < 0) {
799                 err = -EINVAL;
800                 goto out;
801         } else if ((addr = (ulong)shmaddr)) {
802                 if (addr & (SHMLBA-1)) {
803                         if (shmflg & SHM_RND)
804                                 addr &= ~(SHMLBA-1);       /* round down */
805                         else
806 #ifndef __ARCH_FORCE_SHMLBA
807                                 if (addr & ~PAGE_MASK)
808 #endif
809                                         return -EINVAL;
810                 }
811                 flags = MAP_SHARED | MAP_FIXED;
812         } else {
813                 if ((shmflg & SHM_REMAP))
814                         return -EINVAL;
815
816                 flags = MAP_SHARED;
817         }
818
819         if (shmflg & SHM_RDONLY) {
820                 prot = PROT_READ;
821                 acc_mode = S_IRUGO;
822         } else {
823                 prot = PROT_READ | PROT_WRITE;
824                 acc_mode = S_IRUGO | S_IWUGO;
825         }
826         if (shmflg & SHM_EXEC) {
827                 prot |= PROT_EXEC;
828                 acc_mode |= S_IXUGO;
829         }
830
831         /*
832          * We cannot rely on the fs check since SYSV IPC does have an
833          * additional creator id...
834          */
835         ns = current->nsproxy->ipc_ns;
836         shp = shm_lock(ns, shmid);
837         if(shp == NULL) {
838                 err = -EINVAL;
839                 goto out;
840         }
841         err = shm_checkid(ns, shp,shmid);
842         if (err) {
843                 shm_unlock(shp);
844                 goto out;
845         }
846         if (ipcperms(&shp->shm_perm, acc_mode)) {
847                 shm_unlock(shp);
848                 err = -EACCES;
849                 goto out;
850         }
851
852         err = security_shm_shmat(shp, shmaddr, shmflg);
853         if (err) {
854                 shm_unlock(shp);
855                 return err;
856         }
857                 
858         file = shp->shm_file;
859         size = i_size_read(file->f_path.dentry->d_inode);
860         shp->shm_nattch++;
861         shm_unlock(shp);
862
863         down_write(&current->mm->mmap_sem);
864         if (addr && !(shmflg & SHM_REMAP)) {
865                 user_addr = ERR_PTR(-EINVAL);
866                 if (find_vma_intersection(current->mm, addr, addr + size))
867                         goto invalid;
868                 /*
869                  * If shm segment goes below stack, make sure there is some
870                  * space left for the stack to grow (at least 4 pages).
871                  */
872                 if (addr < current->mm->start_stack &&
873                     addr > current->mm->start_stack - size - PAGE_SIZE * 5)
874                         goto invalid;
875         }
876                 
877         user_addr = (void*) do_mmap (file, addr, size, prot, flags, 0);
878
879 invalid:
880         up_write(&current->mm->mmap_sem);
881
882         mutex_lock(&shm_ids(ns).mutex);
883         shp = shm_lock(ns, shmid);
884         BUG_ON(!shp);
885         shp->shm_nattch--;
886         if(shp->shm_nattch == 0 &&
887            shp->shm_perm.mode & SHM_DEST)
888                 shm_destroy(ns, shp);
889         else
890                 shm_unlock(shp);
891         mutex_unlock(&shm_ids(ns).mutex);
892
893         *raddr = (unsigned long) user_addr;
894         err = 0;
895         if (IS_ERR(user_addr))
896                 err = PTR_ERR(user_addr);
897 out:
898         return err;
899 }
900
901 asmlinkage long sys_shmat(int shmid, char __user *shmaddr, int shmflg)
902 {
903         unsigned long ret;
904         long err;
905
906         err = do_shmat(shmid, shmaddr, shmflg, &ret);
907         if (err)
908                 return err;
909         force_successful_syscall_return();
910         return (long)ret;
911 }
912
913 /*
914  * detach and kill segment if marked destroyed.
915  * The work is done in shm_close.
916  */
917 asmlinkage long sys_shmdt(char __user *shmaddr)
918 {
919         struct mm_struct *mm = current->mm;
920         struct vm_area_struct *vma, *next;
921         unsigned long addr = (unsigned long)shmaddr;
922         loff_t size = 0;
923         int retval = -EINVAL;
924
925         if (addr & ~PAGE_MASK)
926                 return retval;
927
928         down_write(&mm->mmap_sem);
929
930         /*
931          * This function tries to be smart and unmap shm segments that
932          * were modified by partial mlock or munmap calls:
933          * - It first determines the size of the shm segment that should be
934          *   unmapped: It searches for a vma that is backed by shm and that
935          *   started at address shmaddr. It records it's size and then unmaps
936          *   it.
937          * - Then it unmaps all shm vmas that started at shmaddr and that
938          *   are within the initially determined size.
939          * Errors from do_munmap are ignored: the function only fails if
940          * it's called with invalid parameters or if it's called to unmap
941          * a part of a vma. Both calls in this function are for full vmas,
942          * the parameters are directly copied from the vma itself and always
943          * valid - therefore do_munmap cannot fail. (famous last words?)
944          */
945         /*
946          * If it had been mremap()'d, the starting address would not
947          * match the usual checks anyway. So assume all vma's are
948          * above the starting address given.
949          */
950         vma = find_vma(mm, addr);
951
952         while (vma) {
953                 next = vma->vm_next;
954
955                 /*
956                  * Check if the starting address would match, i.e. it's
957                  * a fragment created by mprotect() and/or munmap(), or it
958                  * otherwise it starts at this address with no hassles.
959                  */
960                 if ((vma->vm_ops == &shm_vm_ops || is_vm_hugetlb_page(vma)) &&
961                         (vma->vm_start - addr)/PAGE_SIZE == vma->vm_pgoff) {
962
963
964                         size = vma->vm_file->f_path.dentry->d_inode->i_size;
965                         do_munmap(mm, vma->vm_start, vma->vm_end - vma->vm_start);
966                         /*
967                          * We discovered the size of the shm segment, so
968                          * break out of here and fall through to the next
969                          * loop that uses the size information to stop
970                          * searching for matching vma's.
971                          */
972                         retval = 0;
973                         vma = next;
974                         break;
975                 }
976                 vma = next;
977         }
978
979         /*
980          * We need look no further than the maximum address a fragment
981          * could possibly have landed at. Also cast things to loff_t to
982          * prevent overflows and make comparisions vs. equal-width types.
983          */
984         size = PAGE_ALIGN(size);
985         while (vma && (loff_t)(vma->vm_end - addr) <= size) {
986                 next = vma->vm_next;
987
988                 /* finding a matching vma now does not alter retval */
989                 if ((vma->vm_ops == &shm_vm_ops || is_vm_hugetlb_page(vma)) &&
990                         (vma->vm_start - addr)/PAGE_SIZE == vma->vm_pgoff)
991
992                         do_munmap(mm, vma->vm_start, vma->vm_end - vma->vm_start);
993                 vma = next;
994         }
995
996         up_write(&mm->mmap_sem);
997         return retval;
998 }
999
1000 #ifdef CONFIG_PROC_FS
1001 static int sysvipc_shm_proc_show(struct seq_file *s, void *it)
1002 {
1003         struct shmid_kernel *shp = it;
1004         char *format;
1005
1006 #define SMALL_STRING "%10d %10d  %4o %10u %5u %5u  %5d %5u %5u %5u %5u %10lu %10lu %10lu\n"
1007 #define BIG_STRING   "%10d %10d  %4o %21u %5u %5u  %5d %5u %5u %5u %5u %10lu %10lu %10lu\n"
1008
1009         if (sizeof(size_t) <= sizeof(int))
1010                 format = SMALL_STRING;
1011         else
1012                 format = BIG_STRING;
1013         return seq_printf(s, format,
1014                           shp->shm_perm.key,
1015                           shp->id,
1016                           shp->shm_perm.mode,
1017                           shp->shm_segsz,
1018                           shp->shm_cprid,
1019                           shp->shm_lprid,
1020                           is_file_hugepages(shp->shm_file) ? (file_count(shp->shm_file) - 1) : shp->shm_nattch,
1021                           shp->shm_perm.uid,
1022                           shp->shm_perm.gid,
1023                           shp->shm_perm.cuid,
1024                           shp->shm_perm.cgid,
1025                           shp->shm_atim,
1026                           shp->shm_dtim,
1027                           shp->shm_ctim);
1028 }
1029 #endif