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